package P078_ReduceNumbersOfHashMap;

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * @ClassName : ReduceNumbersOfHashMap
 * @Description : 减少HashMap中元素的数量
 * @Author : wukj1
 * @Date: 2022/4/7
 **/
public class ReduceNumbersOfHashMap {

    @Test
    public void test(){
        Map<String, String> map = new HashMap<>();
        final Runtime rt = Runtime.getRuntime();
        //JVM终止前记录内存信息
        rt.addShutdownHook(new Thread(){
            @Override
            public void run() {
                StringBuffer sb = new StringBuffer();
                long heapMaxSize = rt.maxMemory() >> 20;
                sb.append("最大可用内存：" + heapMaxSize + "M\n");
                long total = rt.totalMemory() >> 20;
                sb.append("对内存大小：" + total + "M\n");
                long free = rt.freeMemory() >> 20;
                sb.append("空闲内存：" + free + "M");
                System.out.println(sb);
            }
        });
        Random random = new Random();
        //放入近40万个键值对
        for (int i = 0; i < 393217; i++) {
            map.put("key" + i, "value" + i);
        }
    }

}
